package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.PlanFragmentBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PlanLinkActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_link);
        initTitle();
        initView();
        initData();
    }

    private void initData() {
        uploadLink();
    }

    private List<PlanFragmentBean.InfoEntity> info = new ArrayList<>();

    private void uploadLink() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.TASK_GETPLANNINGLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("----", result + "");
                Gson g = new Gson();
                PlanFragmentBean eban = g.fromJson(result, PlanFragmentBean.class);
                info = eban.getInfo();
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
                Log.e("---222",info.size()+"");
                recycler.setAdapter(new BaseRVAdapter(PlanLinkActivity.this, info) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.item_plan_fragment;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, final int position) {
                        holder.setTextView(R.id.name, info.get(position).getTitle());
                        switch (Integer.parseInt(info.get(position).getType())) {
                            case 0:
                                holder.setTextView(R.id.product_type, "周计划");
                                break;
                            case 1:
                                holder.setTextView(R.id.product_type, "月计划");
                                break;
                            case 2:
                                holder.setTextView(R.id.product_type, "自定义");
                                break;
                        }
                        holder.setTextView(R.id.address, info.get(position).getStart_time() + "——" + info.get(position).getEnd_time());
                        holder.setTextView(R.id.time, info.get(position).getCreate_at());
                        //  0进行中 1已完成 2终止 3未完成 4待点评
                        switch (Integer.parseInt(info.get(position).getStatus())) {
                            case 0:
                                holder.setTextView(R.id.progress, "进行中");
                                break;
                            case 1:
                                holder.setTextView(R.id.progress, "未完成");
                                break;
                            case 2:
                                holder.setTextView(R.id.progress, "已完成");
                                break;
                            case 3:
                                holder.setTextView(R.id.progress, "终止");
                                break;
                            case 4:
                                holder.setTextView(R.id.progress, "待点评");
                                break;
                        }
                        holder.getView(R.id.rl_max).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = getIntent();
                                intent.putExtra("planid", info.get(position).getPlanid());
                                intent.putExtra("name", info.get(position).getTitle());
                                setResult(RESULT_OK, intent);
                                finish();
                            }
                        });
                    }
                });
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });

    }

    private void initView() {
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("关联计划");
        title.setTextColor(getResources().getColor(R.color.main_color));
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
